import com.mochimedia.ui.*;

/**
* ...
* @author $(DefaultUser)
*/

class com.mochimedia.ui.Component extends EventDispatcher implements IComponent {
    
    private var _id:Number = 0;
    public function get id ():Number { return _id; }
    
    private var _type:String = "comp";
    
    private static var _nextID:Number = 0;
    public static function get nextID ():Number { 
        _nextID++;
        return _nextID;
    }
    
    public var name:String = "";
    
    private var _value:String;
    public function get value ():String { return _value; }
    public function set value (val:String):Void { _value = val; } 
   
    public static var EVENT_PRESS:String = "event_press";
    public static var EVENT_RELEASE:String = "event_release";
    public static var EVENT_CLICK:String = "event_click";
    public static var EVENT_ROLLOVER:String = "event_rollover";
    public static var EVENT_ROLLOUT:String = "event_rollout";
    
    public static var EVENT_DRAG:String = "event_drag";
    
    public static var EVENT_FOCUS:String = "event_focus";
    public static var EVENT_BLUR:String = "event_blur";
    public static var EVENT_CHANGE:String = "event_change";

    public static var EVENT_CREATE:String = "event_create";
    public static var EVENT_DESTROY:String = "event_destroy";
    
    public static var globalStyle:Style;
    
    private var _created:Boolean = false;
    public function get created():Boolean { return _created; }
    
    private var _container:MovieClip;
    public function get container():MovieClip { return _container; }
    public function set container(value:MovieClip):Void { _container = value;  }
    
    private var _parentCell:Cell;
    public function get parentCell():Cell { return _parentCell; }
    public function set parentCell(value:Cell):Void { 
        _parentCell = value; 
        if (_parentCell != null) {
            _container = _parentCell.contents;
            create();
        }
        
    }
    
    private var _position:Position;
    public function get position():Position { return _position; }
    public function set position(value:Position):Void { _position = value; }    
    
    public var arranged:Boolean = false;
    
    private var _mc:MovieClip;
    
    public function get tabEnabled ():Boolean { return _mc.tabChildren; }
    public function set tabEnabled (val:Boolean):Void { _mc.tabChildren = val }
    
    private var _style:Style;
    public function get style():Style { return _style; }
    
    private var _active:Boolean = true;
    public function get active():Boolean { return _active; }
    public function set active(value:Boolean):Void { _active = value; }
    
    private var _enabled:Boolean = true;
    public function get enabled():Boolean { return _enabled; }
    public function set enabled(value:Boolean):Void { _enabled = value; }
        
    public function get x ():Number { return _mc._x; }
    public function set x (val:Number):Void { if (!isNaN(val)) _mc._x = Math.floor(val); }
    
    public function get y ():Number { return _mc._y; }
    public function set y (val:Number):Void { if (!isNaN(val)) _mc._y = Math.floor(val); }
    
    public function get zindex ():Number { return _position.zindex; }
    
    public function get scale ():Number { return _mc._xscale / 100; }
    public function set scale (val:Number):Void { _mc._xscale = _mc._yscale = val * 100 }
    
    public function get visible ():Boolean { return _mc._visible; }
    
    private var _width:Number = 0;
    private var _height:Number = 0;
    
    public function get width ():Number { return _width; }
    public function get height ():Number { return _height; }

    //
    //
    public function Component (container:MovieClip, position:Position, style:Style) {
        
    }
    
    //
    //
    private function init (container:MovieClip, position:Position, style:Style):Void {
        
        super.init();
        
        _id = nextID;
        
        _container = (container != undefined) ? container : null;
        
        var defaultOptions:Object = { };
        
        if (_type == "formfield" || _type == "htmlfield" || _type == "combobox" || _type == "slider" || _type == "hrule") {
        
            defaultOptions.margins = [0, 0, 15, 0];
            
        }
        
        _position = (position != undefined) ? position : new Position( defaultOptions );
        _style = (style != undefined) ? style : (globalStyle != undefined) ? globalStyle : new Style(); 

    }
    
    //
    //
    public function create ():Void {
        
        if (created) return;
        
        _mc = _container.createEmptyMovieClip(_type + "_" + _container.getNextHighestDepth() + "@" + getTimer() + Number(Math.floor(Math.random() * 16000000)).toString(16), _container.getNextHighestDepth());
        
        _created = true;
        
    }
    
    //
    //
    public function onClick ():Void {
        
        dispatchEvent(EVENT_CLICK);
        
    }
    
    //
    //
    public function onRollOver ():Void {
        
        dispatchEvent(EVENT_ROLLOVER);
        
    }
    
    //
    //
    public function onRollOut ():Void {

        dispatchEvent(EVENT_ROLLOUT);
        
    }
    
    private var _target:Component;
    private var _targetEvent:String;
    private var _targetProperty:String;

    //
    //
    public function connect (event:String, target:Component, property:String):Void {
        
        if (_target != null && _targetEvent != null) _target.removeEventListener(_targetEvent, this, onTargetEvent);
        
        _target = target;
        _targetEvent = event;
        _targetProperty = property;
        
        if (_target != null) {
            
            _target.addEventListener(_targetEvent, this, onTargetEvent);
            onTargetEvent();
            
        }
        
    }
    
    //
    //
    public function onTargetEvent (e:Event):Void {
    
        if (typeof(_target[_targetProperty]) == "function") {
        
            if (_target[_targetProperty]() != false) enable();
            else disable();     
        
        } else {
        
            if (_target[_targetProperty] != false) enable();
            else disable();
            
        }
        
    }
    
    //
    //
    private function connectButton (btn:MovieClip, dragMode:Boolean):Void {
        
        var self:Object = this;
        
        btn.onRollOver = function ():Void { self.dispatchEvent(Component.EVENT_ROLLOVER); }
        btn.onRollOut = function ():Void { self.dispatchEvent(Component.EVENT_ROLLOUT); }
        
        
        if (dragMode != true) {
            btn.onRelease = function ():Void { self.dispatchEvent(Component.EVENT_CLICK); }
            btn.onDragOut = function ():Void { self.dispatchEvent(Component.EVENT_ROLLOUT); }
        } else {
            btn.onPress = function ():Void { self.dispatchEvent(Component.EVENT_PRESS); }
            btn.onRelease = function ():Void { self.dispatchEvent(Component.EVENT_RELEASE); }
            btn.onReleaseOutside = function ():Void { self.dispatchEvent(Component.EVENT_ROLLOUT); self.dispatchEvent(Component.EVENT_RELEASE); }
        }

    }
    
    //
    //
    private function connectTextField (tf:TextField):Void {
        
        var self:Object = this;
        
        tf.onChanged = function ():Void { self.dispatchEvent(Component.EVENT_CHANGE); }
        tf.onSetFocus = function ():Void { self.dispatchEvent(Component.EVENT_FOCUS); }
        tf.onKillFocus = function ():Void { self.dispatchEvent(Component.EVENT_BLUR); }

    }
    
    //
    //
    public function enable ():Void {
        
        _enabled = true;
        
    }
    
    //
    //
    public function disable ():Void {

        _enabled = false;
        
    }
    
    //
    //
    public function show ():Void {
        
        _mc._visible = true;
        dispatchEvent(EVENT_FOCUS);
        
    }
    
    //
    //
    public function hide ():Void {
        
        _mc._visible = false;
        dispatchEvent(EVENT_BLUR);
        
    }
    
    //
    //
    public function toggle ():Void {
        
        if (_mc._visible) hide();
        else show();
        
    }
    
    //
    //
    public function destroy ():Boolean {
        
        if (_mc == null) return false;
        
        _mc.removeMovieClip();
        _mc = null;
        
        dispatchEvent(EVENT_DESTROY);
        
        _listeners = null;
        
        return true;
        
    }
    
}